package cn.zcf.pet.service.impl;

import cn.zcf.basic.service.impl.BaseServiceImpl;

import cn.zcf.pet.domain.PetType;
import cn.zcf.pet.mapper.PetTypeMapper;
import cn.zcf.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper mapper;
    @Override
    public List<PetType> petTree() {
        List<PetType> all = mapper.loadAll();
        HashMap<Long, PetType> map = new HashMap<>();
        for (PetType type : all) {
            map.put(type.getId(),type);
        }
        List<PetType> typeTree = new ArrayList<>();
        for (PetType type : all) {
            if(type.getPid()==null){
                typeTree.add(type);
            }
            else {
                PetType parentPetType = map.get(type.getPid());
                parentPetType.getChildren().add(type);
            }
        }
        return typeTree;
    }
}
